/*
 * @Date: 2025-11-07 17:12:39
 * @Auth: 873768511@qq.com
 * @LastEditors: 873768511@qq.com
 * @LastEditTime: 2025-11-10 17:31:48
 * @FilePath: \corpus\src\pages\systemManagement\role\hooks\useMenuTree.ts
 */
import { useMemo, useState } from 'react';

import { useRequest } from 'ahooks';

import { postMenuFindWholeTree } from '@/services/systemManagement/menu';

import { flatTree } from '../util';

import type { IMenuTree } from '@/types/systemManagement/menu';

export default () => {

  const [checkedKeys, setCheckedKeys] = useState<number[]>([]);
  const [allPageList, setAllPageList] = useState<IMenuTree[]>([]);

  const defaultPageList = useMemo(() => {
    return allPageList.filter((item) => checkedKeys.includes(item.id));
  }, [checkedKeys, allPageList]);

  const roleTree = useRequest(async () => {
    const res = await postMenuFindWholeTree();
    if (res?.data) {
      setAllPageList(flatTree(res.data).filter((item) => item.menuType.code === 'M'));
      return res.data;
    }
    return [];
  });

  return {
    roleTree,
    checkedKeys,
    setCheckedKeys,
    defaultPageList,
  };
};
